package com.woniuxy.woniuhis.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.woniuxy.woniuhis.entity.Deposit;
import com.woniuxy.woniuhis.entity.Examination;
import com.woniuxy.woniuhis.entity.Inpatient;
import com.woniuxy.woniuhis.entity.PayIn;
import com.woniuxy.woniuhis.entity.RecipeMedi;
import com.woniuxy.woniuhis.entity.SurgeryExe;
import com.woniuxy.woniuhis.exception.DepositServiceException;
import com.woniuxy.woniuhis.exception.PayInServiceException;
import com.woniuxy.woniuhis.mapper.DepositMapper;
import com.woniuxy.woniuhis.mapper.InpatientMapper;
import com.woniuxy.woniuhis.mapper.PayInMapper;
import com.woniuxy.woniuhis.service.DepositService;
@Service
public class DepositServiceImpl implements DepositService{
	@Autowired
	DepositMapper depositMapper;
	@Autowired
	PayInMapper payInMapper;
	@Autowired
	InpatientMapper inpatientMapper;
	@Override
	//添加押金
	public void addDeposit(Deposit deposit,PayIn payIn) {
		try {
			//添加押金的同时生成一个订单
			depositMapper.insertDeposit(deposit);
			payInMapper.insertPayIn(payIn);
		} catch (Exception e) {
			e.printStackTrace();
			throw new DepositServiceException();
		}	
	}

	@Override
	//删除押金
	public void delDepositById(String depositId) {
		try {
			depositMapper.deleteDepositById(depositId);
		} catch (Exception e) {
			e.printStackTrace();
			throw new DepositServiceException();
		}	
	}

	@Override
	//修改押金
	public void modifyDeposit(Deposit deposit) {
		try {
			depositMapper.rechargeDeposit(deposit);
		} catch (Exception e) {
			e.printStackTrace();
			throw new DepositServiceException();
		}			
	}
	
	@Override
	//充值押金的时候需要插入一个订单
	public void modifyDepositAndPayIn(Deposit deposit, PayIn payIn) {
		try {
			depositMapper.rechargeDeposit(deposit);
			payInMapper.insertPayIn(payIn);
		} catch (Exception e) {
			e.printStackTrace();
			throw new DepositServiceException();
		}	
	}
	@Override
	public Deposit findDepositById(String depositId) {
		try {
			Deposit deposit = depositMapper.selectDepositById(depositId);
			return deposit;
		} catch (Exception e) {
			e.printStackTrace();
			throw new DepositServiceException();
		}	
	}

	@Override
	//查找押金
	public List<Deposit> findDeposit(Deposit deposit) {
		try {
			List<Deposit> deposits = depositMapper.selectDeposit(deposit);
			return deposits;
		} catch (Exception e) {
			e.printStackTrace();
			throw new DepositServiceException();
		}	
	}

	@Override
	//押金加减
	public void rechargeDeposit(Deposit deposit) {
		try {
			depositMapper.rechargeDeposit(deposit);
		} catch (Exception e) {
			e.printStackTrace();
			throw new DepositServiceException();
		}	
	}

	@Override
	//通过病人id查询押金
	public Deposit findDepositByInpaientId(String inpaientId) {
		try {
			Deposit deposit = depositMapper.selectDepositByInpaientId(inpaientId);
			return deposit;
		} catch (Exception e) {
			e.printStackTrace();
			throw new DepositServiceException();
		}	
	}
	
	
	@Override
	//查询病人押金列表
	public List<Inpatient> findInpatientDeposit(Inpatient condition, Date beginDate, Date endDate) {
		try {
			List<Inpatient> inp = inpatientMapper.selectInpatientByPay(condition, beginDate, endDate);
			//循环判断，如果没有押金记录就删除
			for(int i=0;i<inp.size();i++) {
				Deposit deposit = depositMapper.selectDepositByInpaientId(inp.get(i).getInpatientId());
				if(deposit!=null) {
					Double debt=deposit.getBalance().doubleValue();
					inp.get(i).setInpatientFamlilyTel(debt.toString());
				}else {
					inp.remove(i);
					i--;
				}
			}
			return inp;
		} catch (Exception e) {
			e.printStackTrace();
			throw new DepositServiceException();
		}
	}


}
